<?php

namespace App\Kuafu\WaiMai\Controller\Ext;

use App\Kuafu\Base\Controller\BaseController;
use App\Kuafu\Order\Enum\CodeEnum;
use App\Kuafu\WaiMai\Enum\DyWaiMaiEnum;
use App\Kuafu\WaiMai\Service\BaseWaiMaiOrderService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class DyWaiMaiCallbackController extends BaseController
{
    private       $orderService;
    private array $requestData;

    // 抖音通用返回成功
    private function DySuccess(): string
    {
        return '{"data":"ok"}';
    }

    private function prepare(Request $request, string $functionName)
    {
        $this->orderService = BaseWaiMaiOrderService::getIns(CodeEnum::SOURCE_TYPE_DY)->init();
        $this->requestData  = $request->all();
        Log::channel('waiMai')->info("DyWaiMai Callback {$functionName}:", $this->requestData);
    }

    // 推送 根据event区分业务 todo 异步处理
    public function push(Request $request): string
    {
        try {
            $this->prepare($request, __FUNCTION__);
            $event = (string)$request->get('event', '');//消息类型，用于区分各类消息
            switch ($event) {
                case DyWaiMaiEnum::PAY_SUCCESS_ORDER:
                    $this->orderService->createOrder();//用户下单消息
                    break;
                case DyWaiMaiEnum::RECEIVE_MERCHANT_ORDER:
                    $this->orderService->acceptOrder();//订单已接单消息
                    break;
                case DyWaiMaiEnum::REFUSE_MERCHANT_ORDER:
                    $this->orderService->refuseOrder();//订单已拒单消息
                    break;
                case DyWaiMaiEnum::COMPLETE_REFUND_ORDER:
                    $this->orderService->cancelOrder();//订单已取消消息
                    break;
                case DyWaiMaiEnum::AUDIT_REFUND_ORDER:
                    $this->orderService->auditRefundOrder();//订单已同意/已拒绝退款消息
                    break;
                case DyWaiMaiEnum::CHANGE_DELIVERY_ORDER:
                    $this->orderService->deliveringOrder();//平台配送-配送状态变更消息
                    break;
                default:
                    Log::channel('waiMai')->info("DyWaiMai Callback event empty :", $this->requestData);
            }
        } catch (\Exception $exception) {
            Log::channel('waiMai')->error("DyWaiMai Callback push err :" . $exception->getMessage());
        }
        return $this->DySuccess();
    }

    /**
     * 用户申请退款消息
     * 1.当用户发起退款申请，且需要商家审核时，需要通过此接口发消息给第三方系统。
     * 2.收到用户申请退款消息后，可立即返回处理结果：同意、拒绝、处理中；也可先返回“处理中”，然后再异步处理。建议先返回“处理中”，待商家处理后再异步通过「商家同意/拒绝退款接口」同步处理结果。
     * @param Request $request
     * @return array
     */
    public function takeoutRefundNotice(Request $request): array
    {
        try {
            $this->prepare($request->merge(['event' => DyWaiMaiEnum::REFUND_ORDER_NOTICE]), __FUNCTION__);
//            $this->orderService->applyCancelOrder();//用户申请退款消
        } catch (\Exception $exception) {
            Log::channel('waiMai')->error("DyWaiMai Callback takeoutRefundNotice err :" . $exception->getMessage());
        }
        return [
            "data" => [
                "result"     => -1,//-1 处理中 2 审核通过 3 审核拒绝
                /*"reject_reason" => [//商家拒绝退款原因
                    "reason_code" => [123, 234],//退款原因code
                    "desc"        => "不想要了",//商家可填写的备注信息
                    "show_reason" => [//错误码对应的文案
                        [
                            "reason_code" => "123",//退款原因code
                            "msg"         => "货品不足",//退款原因文案
                        ],
                    ]
                ],*/
                "error_code" => 0,//错误码
            ]
        ];
    }

}
